/*
* $Id: MainSettings.cpp 2 2009-12-23 13:58:02Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "MainSettings.h"
#include "SettingControlIds.h"
#include "DinIntDefined.h"
#include <WinSpool.h>

#define MAIN_REG_NAME	_T("Software\\Apiary\\MainSettings")

CMainSetting::CMainSetting()
: CSettings(SETTINGS_ID, MAIN_REG_NAME, TRUE){
	Init();
}
CMainSetting::~CMainSetting(){}

std::vector<Window> CMainSetting::GetWindows(){
	std::vector<Window> aWindow;
	aWindow.push_back(Window(STATIC, 10, 0, 415, 23, SS_CENTER, IApplication::GetApp()->GetLanguageManager()->GetString(SETTINGS_ID), STATIC_HEADER_ID));
	aWindow.push_back(Window(STATIC, 10, 30, 415, 15, SS_LEFT,	IApplication::GetApp()->GetLanguageManager()->GetString(LANGUAGE_ID),STATIC_LANG_ID));
	aWindow.push_back(Window(COMBO_BOX, 10, 45, 415, 20, CBS_DROPDOWNLIST, IApplication::GetApp()->GetLanguageManager()->GetDefaultLanguage(),	COMBO_LANG_ID));
	aWindow.push_back(Window(STATIC, 10, 75, 415, 15, SS_LEFT,	IApplication::GetApp()->GetLanguageManager()->GetString(PRINTER_ID), STATIC_PRINTER_ID));
	aWindow.push_back(Window(COMBO_BOX, 10, 90, 415, 20, CBS_DROPDOWNLIST, _T(""), COMBO_PRINTER_ID));
	return aWindow;
}
std::map<std::tstring, std::tstring> CMainSetting::GetStates(){
	std::map<std::tstring, std::tstring> aMapStates;
	aMapStates.insert(aMapStates.end(), std::make_pair(STATIC_HEADER_ID, IApplication::GetApp()->GetLanguageManager()->GetString(SETTINGS_ID)));
	aMapStates.insert(aMapStates.end(), std::make_pair(STATIC_LANG_ID, IApplication::GetApp()->GetLanguageManager()->GetString(LANGUAGE_ID)));
	aMapStates.insert(aMapStates.end(), std::make_pair(STATIC_PRINTER_ID, IApplication::GetApp()->GetLanguageManager()->GetString(PRINTER_ID)));
	aMapStates.insert(aMapStates.end(), std::make_pair(COMBO_LANG_ID, CCommonFunction::GetRegValue(MAIN_REG_NAME, COMBO_LANG_ID)));
	aMapStates.insert(aMapStates.end(), std::make_pair(COMBO_PRINTER_ID, CCommonFunction::GetRegValue(MAIN_REG_NAME, COMBO_PRINTER_ID)));
	return aMapStates;
}
std::map<std::tstring, std::tstring> CMainSetting::GetDefaultStates(){
	std::map<std::tstring, std::tstring> aMapStates;
	aMapStates.insert(aMapStates.end(), std::make_pair(COMBO_LANG_ID, IApplication::GetApp()->GetLanguageManager()->GetDefaultLanguage()));
	DWORD size = 1000*sizeof(TCHAR);
	TCHAR val1[1000];
	memset(val1, 0, size);
	GetDefaultPrinter(val1, &size);
	aMapStates.insert(aMapStates.end(), std::make_pair(COMBO_PRINTER_ID, val1));
	return aMapStates;
}
std::map<std::tstring, std::vector<Parameter> > CMainSetting::GetParameters(){
	std::map<std::tstring, std::vector<Parameter> > aMapParameters;
	std::vector<Parameter> aParameters;

	//static_header                                  
	aParameters.push_back(Parameter(TYPE, HEADER));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(STATIC_HEADER_ID, aParameters));

	//static_lang
	aParameters.clear();
	aMapParameters.insert(aMapParameters.end(), std::make_pair(STATIC_LANG_ID, aParameters));

	//combo_lang
	std::vector<std::tstring> aLanguage = IApplication::GetApp()->GetLanguageManager()->GetSupportLanguage();
	for (std::vector<std::tstring>::iterator it = aLanguage.begin(); it != aLanguage.end(); it++)	
		aParameters.push_back(Parameter(COMBO_STRING, *it));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(COMBO_LANG_ID, aParameters));

	//static_printer
	aParameters.clear();
	aMapParameters.insert(aMapParameters.end(), std::make_pair(STATIC_PRINTER_ID, aParameters));

	//combo_printer
	PRINTER_INFO_1A* printerInfo = NULL;
	DWORD nNeaded, nPrinters;
	EnumPrintersA(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 1, (LPBYTE)printerInfo, 0, &nNeaded, &nPrinters);
	if(nNeaded > 0){
		printerInfo = (PRINTER_INFO_1A*)GlobalAlloc(GPTR, nNeaded);
		EnumPrintersA(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 1, (LPBYTE)printerInfo, nNeaded, &nNeaded, &nPrinters);
		USES_CONVERSION;
		for (int i = 0; i < (int)nPrinters; i++)
			aParameters.push_back(Parameter(COMBO_STRING, A2T(printerInfo[i].pName)));
		GlobalFree(printerInfo);
	}
	aMapParameters.insert(aMapParameters.end(), std::make_pair(COMBO_PRINTER_ID, aParameters));

	return aMapParameters;
}
void CMainSetting::PostSave(){
//	IApplication::GetApp()->GetChangeManager()->SetChange(SETTINGS, CHANGE_GENERAL_SETTINGS);
}